#include "output_handler.h"

#include "Arduino.h"
#include "constants.h"

int led = 16;
bool initialized = false;

void HandleOutput(tflite::ErrorReporter* error_reporter, float x_value,
                  float y_value) {

      // Do this only once
        if (!initialized) {
          ledcSetup(0, 5000, 13);
          // Set the LED pin to output
          ledcAttachPin(led, 0);
          pinMode(led, OUTPUT);
          initialized = true;
        }
      
        // Calculate the brightness of the LED such that y=-1 is fully off
        // and y=1 is fully on. The LED's brightness can range from 0-255.
        int brightness = (int)(127.5f * (y_value + 1));
      
        // Set the brightness of the LED. If the specified pin does not support PWM,
        // this will result in the LED being on when y > 127, off otherwise.
        analogWrite(led, brightness);

        // uint32_t duty = (8191 / 255) * min(brightness, 255);
        // ledcWrite(0, duty);

        // delay(10);

        
      
        // Log the current brightness value for display in the Arduino plotter
        TF_LITE_REPORT_ERROR(error_reporter, "%d\n", brightness);

                    
 // Log the current X and Y values
 TF_LITE_REPORT_ERROR(error_reporter, "x_value: %f, y_value: %f\n",
                      static_cast<double>(x_value),
                      static_cast<double>(y_value));
}
